//计算hash
import sparkMd5 from "spark-md5";
export const cumulateHash = (
  blobs: { hash: number; blob: Blob }[],
  sliceSize: number
) => {
  return new Promise<string>((resolve) => {
    const result = [] as Blob[];
    const spark = new sparkMd5.ArrayBuffer();
    const fileReader = new FileReader();
    blobs.forEach((item: any, index: number) => {
      if (index === 0 || index === blobs.length - 1) {
        result.push(item);
      } else {
        //取文件前两个字符 最后两个字符 中间两个字符
        result.push(item.blob.slice(0, 2));
        result.push(item.blob.slice(sliceSize / 2 - 1, sliceSize / 2 + 1));
        result.push(item.blob.slice(sliceSize - 2, sliceSize));
      }
    });
    fileReader.readAsArrayBuffer(new Blob(result));
    fileReader.onload = (e: any) => {
      spark.append((e.target as any).result);
      resolve(spark.end());
    };
  });
};
